package com.sangeng.util;

import org.springframework.beans.BeanUtils;

import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;

public class BeanCopyUtils {

    private BeanCopyUtils(){}

    public static <V> V copyBean(Object resource,Class<V> resultClass){
        V result = null;
        try {
          result =  resultClass.newInstance();
          //使用spring的BeanUtils.copyProperties()实现拷贝
            BeanUtils.copyProperties(resource,result);
        } catch (Exception e) {
            e.printStackTrace();
        }
        //将拷贝后的结果返回
        return result;
    }

    public static <O,V> List<V> copyBean(List<O> resource, Class<V> resultClass){
    return  resource.stream()
                .map(o -> copyBean(o,resultClass))
                .collect(Collectors.toList());
    }


}
